<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" destroy-on-close @closed="$emit('closed')" align-center fullscreen>
        <el-container>
            <el-main>
                <el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right" style="height:700px;overflow:auto">
                    <el-row :gutter="1">
                        <el-col :span="6">
                            <el-form-item label="承制人">
								<el-input v-model="form.apply_name" placeholder="承制人" readonly />
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="结算月份" prop="date">
                                <el-date-picker v-model="form.date" :value-format="'YYYY-MM-DD'" type="data" style="width:100%" placeholder="请选择结算月份" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="结算单号" prop="statement_no">
                                <el-input v-model="form.statement_no" placeholder="结算单号" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="1">
                        <el-col :span="5">
                            <el-form-item label="应付">
                                <el-input v-model="form.total_amount" placeholder="应付" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="5">
                            <el-form-item label="实付">
                                <el-input v-model="form.pay_amount" placeholder="实付" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="5">
                            <el-form-item label="补扣">
                                <el-input v-model="form.deduction_amount" placeholder="补扣" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="5">
                            <el-form-item label="备注">
                                <el-input v-model="form.remark" placeholder="请输入备注" />
                            </el-form-item>
                        </el-col>
                        <el-button :loading="isSaveing" @click="count" type="primary" style="margin-left:50px">计算补扣</el-button>
                    </el-row>
                    <el-col :span="24">
                        <el-form-item label="合同信息">
                            <sc-form-table ref="table" v-model="form.contract_data" :addTemplate="addTemplate" placeholder="暂无数据" height="500" hideAdd hideDelete>
                                <el-table-column label="缸号" align="center">
                                    <template #default="scope">
                                        <div>{{scope.row.batch_number}}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column label="发织合同号" prop="contract_no" align="center"></el-table-column>
                                <el-table-column label="工序名" prop="process_title" align="center"></el-table-column>
                                <el-table-column label="发织数" prop="weaving_num" align="center"></el-table-column>
                                <el-table-column label="收货数" prop="take_num" align="center"></el-table-column>
                                <el-table-column label="返工数" prop="rework_num" align="center"></el-table-column>
                                <el-table-column label="待收" prop="pending_num" align="center"></el-table-column>
                                <el-table-column label="收货重" prop="take_weight" align="center"></el-table-column>
                                <el-table-column label="单价" prop="price" align="center"></el-table-column>
                                <el-table-column label="单位" prop="unit_name" align="center"></el-table-column>
                                <el-table-column label="应付" prop="total_amount" align="center"></el-table-column>
                                <el-table-column label="补/扣" prop="deduction_amount" align="center">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.deduction_amount" @input="deInput(scope.row)"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column label="实付" prop="pay_amount" align="center"></el-table-column>
                                <el-table-column label="货期" prop="delivery_time" align="center"></el-table-column>
                                <el-table-column label="末次收货" prop="last_take_date" align="center"></el-table-column>
                                <el-table-column label="备注" prop="remark" align="center">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.remark"></el-input>
                                    </template>
                                </el-table-column>
								<!-- <el-table-column>
								  <template #default="scope">
									  <el-button type="primary" size="small" @click="openMaterial(scope.row,scope.$index)">查看毛种</el-button>
								  </template>
							  </el-table-column> -->
                            </sc-form-table>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-main>
            <el-footer>
                <div style="display:flex;justify-content: end;">
                    <el-button @click="visible=false" >取消</el-button>
                    <el-button :loading="isSaveing" @click="submit(1)" type="primary">保存</el-button>
                    <el-button :loading="isSaveing" @click="submit(2)" type="primary">保存并确认</el-button>
                </div>
            </el-footer>
        </el-container>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '补扣款',
					edit: '完善结算单',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
                grade:"潜力",
                //表单数据
				form: {
                    statement_no:"",
                    confirm_status:"",
                    remark:"",
                    supplier_id:"",
                    contract_data:[],
                    pay_amount:"",
                    deduction_amount:"",
                    process_id: 8,
                    date: new Date().toISOString().slice(0, 10)
				},
                info:{
                    total_amount:"",
                },
                addTemplate:{},
                applyList:[],
                selection: [],
                array:[],
                materialData:[],
			}
		},
		mounted() {
            this.getApply()
        },
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
            //表单提交方法
			submit(e){
                this.form.confirm_status = e
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						res = await this.$API.produce.waitSettled.perfectStatement.post(this.form);
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			async setData(data){
				this.form.apply_name = data.apply_name
                var res = await this.$API.produce.waitSettled.detail.get({statement_no:data.statement_no});
                let array = []
                res.data.contractDetail.map(item=>{
                    array.push({
                        id:item.id,
                        batch_number:item.batch_number,
                        contract_no:item.contract_no,
                        process_title:item.process_title,
                        price:item.price,
                        unit_name:item.unit_name,
                        weaving_num:item.weaving_num,
                        take_num:item.take_num,
                        pending_num:item.pending_num,
                        rework_num:item.rework_num,
                        total_amount:item.total_amount,
                        pay_amount:item.pay_amount,
                        take_weight:item.take_weight,
                        delivery_time:item.delivery_time,
                        last_take_date:item.last_take_date,
                        remark:item.remark,
                        deduction_amount:item.deduction_amount,
                    })
                    this.form.contract_data = array
                })
                this.form.total_amount = res.data.total_amount
                this.form.pay_amount = res.data.goods_amount
                this.form.deduction_amount = res.data.deduction_amount
                this.form.supplier_id = res.data.supplier_id
                this.form.remark = res.data.remark
                this.form.statement_no = data.statement_no
				this.count()
			},
            matInput(contract_no){
                let num = 0
                this.form.material_data.map(item=>{
                    num += Number(item.deduction_amount)
                })
                this.array[contract_no].materialNum = num
            },
            deInput(row){
                row.pay_amount = Number(row.total_amount) + Number(row.deduction_amount)
            },
            async getApply(){
                var res = await this.$API.all.Apply.get();
                this.applyList = res.data;
            },
			/**查看毛种*/
			// openMaterial(row){
            //     this.form.material_data = row.materialData
            // },
			/**计算补扣*/
            count(){
                let y = 0;
				this.form.contract_data.map((item=> {
		            y = Number(item.deduction_amount) + Number(y);
	            }))
                this.form.deduction_amount = Number(y)
                this.form.pay_amount = (Number(this.form.total_amount) + this.form.deduction_amount).toFixed(2)
            },
		}
	}
</script>

<style lang="scss"></style>
